﻿using System.Collections.Generic;
using System.Linq;
using NeuralStock.DataAccess;
using NeuralStock.DomainService.Interfaces;
using NeuralStock.Entities;

namespace NeuralStock.DomainService.Implementation
{
    public class MacroDataService : BaseService, IMacroDataService
    {
        public IEnumerable<MacroData> GetAllMacroDataValues()
        {
            return DbContext.MacroData.OrderBy(x => x.Name).ToList();
        }

        public MacroData GetMacroData(int macroDataId)
        {
            return DbContext.MacroData.FirstOrDefault(x => x.Id == macroDataId);
        }

        public MacroData GetMacroData(string macroDataName)
        {
            return DbContext.MacroData.FirstOrDefault(x => x.Name == macroDataName);
        }

        public IEnumerable<MacroDataQuotation> GetMacroDataQuotations(int macroDataId)
        {
            return DbContext.MacroDataQuotation.Where(x => x.MacroDataId == macroDataId).ToList();
        }

        public IEnumerable<MacroDataQuotation> GetMacroDataQuotations(int macroDataId, int year)
        {
            return DbContext.MacroDataQuotation.Where(x => x.MacroDataId == macroDataId && x.Year == year).ToList();
        }

        public IEnumerable<MacroDataQuotation> GetMacroDataQuotations(int macroDataId, int yearFrom, int yearTo)
        {
            return DbContext.MacroDataQuotation.Where(x => x.MacroDataId == macroDataId && x.Year >= yearFrom && x.Year <= yearTo).ToList();
        }

        public MacroDataQuotation GetMacroDataQuotation(int macroDataQuotationId)
        {
            return DbContext.MacroDataQuotation.FirstOrDefault(x => x.Id == macroDataQuotationId);
        }

        public MacroDataQuotation GetMacroDataQuotation(int macroDataId, int year, int month)
        {
            return DbContext.MacroDataQuotation.FirstOrDefault(
                    x => x.MacroDataId == macroDataId
                    && x.Year == year
                    && x.Month == month);
        }

        public void CreateQuotation(int macroDataId, MacroDataData macroData)
        {
            if (macroData.IsValid())
            {
                MacroDataQuotation quotation = new MacroDataQuotation()
                {
                    MacroDataId = macroDataId,
                    Year = macroData.Year.Value,
                    Month = macroData.Month.Value,
                    Value = macroData.Value.Value
                };

                if (quotation != null)
                {
                    DbContext.MacroDataQuotation.AddObject(quotation);
                    DbContext.SaveChanges();
                }
            }
        }

        public void UpdateQuotation(int macroDataId, MacroDataData macroData)
        {
            if (macroData.IsValid())
            {
                MacroDataQuotation quotation = GetMacroDataQuotation(macroDataId, macroData.Year.Value, macroData.Month.Value);

                if (quotation != null)
                {
                    quotation.Value = macroData.Value.Value;
                    DbContext.SaveChanges();
                }
            }
        }

        public void DeleteQuotation(int macroDataQuotationId)
        {
            MacroDataQuotation quotation = GetMacroDataQuotation(macroDataQuotationId);

            if (quotation == null)
            {
                DbContext.MacroDataQuotation.DeleteObject(quotation);
                DbContext.SaveChanges();
            }
        }
    }
}
